package com.virus.coronavirus.service;

import com.virus.coronavirus.error.SystemCodeEnum;
import com.virus.coronavirus.error.SystemException;
import com.virus.coronavirus.mapper.AreaMapper;
import com.virus.coronavirus.model.Area;
import com.virus.coronavirus.model.vo.AreaVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.reflection.ArrayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author: Bbeginner
 * @Date: 2021/2/4 14:10
 * @Version 1.0
 **/
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaMapper areaMapper;

    @Override
    public List<Area> selectList() {
        return areaMapper.selectAll();
    }


    /**
     * 删除中高风险地区
     * @param placename 中高风险地区名
     */
    @Transactional
    @Override
    public void deleteByPlace(String placename) throws SystemException {
        Example example = new Example(Area.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("placename",placename);
        List<Area> area=areaMapper.selectByExample(example);
        if(area==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除的风险地区不存在");
        }
        areaMapper.deleteByExample(example);
    }

//    //根据经纬度查找某范围内的中高风险地区
//    @Transactional
//    @Override
//    public List<Area> selectAreaBylatlng(Double range, Double lng1, Double lat1,Double lng2, Double lat2,Double lng3, Double lat3){
//        List<Area> areas=areaMapper.selectAll();
//        List<Area> arealist = new ArrayList<Area>();
//        for(Area area:areas){
//            Double Lng1=area.getLng1();
//            Double Lng2=area.getLng2();
//            Double Lng3=area.getLng3();
//            Double Lat1=area.getLat1();
//            Double Lat2=area.getLat2();
//            Double Lat3=area.getLat3();
//            if((Lng1-lng1)<range)
//                if((Lng2-lng2)<range)
//                    if((Lng3-lng3)<range)
//                        if((Lat1-lat1)<range)
//                            if((Lat2-lat2)<range)
//                                if((Lat3-lat3)<range){
//                                    arealist.add(area);
//                                }
//        }
//        return arealist;
//    }




    //新增中高风险地区
    @Override
    public void add(AreaVO areaVO) throws SystemException {
        Long id=areaVO.getId();
        @NotBlank(message = "中高风险地区名不能为空")
        String placename=areaVO.getPlacename();
        @NotNull(message = "中高风险地点所在纬度不能为空")
        Double clat=areaVO.getClat();
        @NotNull(message = "中高风险地点所在经度不能为空")
        Double clng=areaVO.getClng();
        @NotBlank(message = "组成中高风险地区的经纬度不能为空")
        Double[] rlnglat1=areaVO.getRlnglat();
        Double[] rlng1=new Double[rlnglat1.length/2];
        Double[] rlat1=new Double[rlnglat1.length/2];
        for(int i=0;i<rlnglat1.length;i++){
            if(i%2==0){
                rlng1[i/2]=rlnglat1[i];
            }
            else{
                rlat1[(i-1)/2]=rlnglat1[i];
            }
        }
        String conjunction=",";
        String rlng= StringUtils.join(rlng1,conjunction);
        String rlat= StringUtils.join(rlat1,conjunction);
        Area area = new Area();
        area.setId(id);
        area.setClat(clat);
        area.setClng(clng);
        area.setRlat(rlat);
        area.setRlng(rlng);
        area.setPlacename(placename);
        //创造一个例子，判断此地区是否存在
        Example o = new Example(Area.class);
        o.createCriteria().andEqualTo("placename",placename);
        int i=areaMapper.selectCountByExample(o);
        if (i!=0) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR, "该地区已被列为中高风险地区");
        }
        areaMapper.insert(area);
    }

    //更新风险区域   // 不写edit了，直接用find那个
    @Override
    public void update(String placename,String boundary) throws SystemException {
//        Example example = new Example(Area.class);
//        Example.Criteria criteria=example.createCriteria();
//        criteria.andEqualTo("placename",placename);
//        List<Area> area=areaMapper.selectByExample(example);
//        if(area==null){
//            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要编辑的风险地区不存在");
//        }

        Example example = new Example(Area.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("placename","%"+placename+"%");
        Area area=areaMapper.selectOneByExample(example);
        String[] split = boundary.split(",");
        ArrayList<String> rlat= new ArrayList<>();
        ArrayList<String> rlng= new ArrayList<>();
        for (int i=2;i<split.length;i++){
            if (i%2==0){
                rlat.add(split[i]);
            }else {
                rlng.add(split[i]);
            }
        }
        area.setClat(Double.valueOf(split[0]));
        area.setClng(Double.valueOf(split[1]));
        String s1=rlat.toString();
        String s2=rlng.toString();
        area.setRlat(s1.substring(1,s1.length()-1));
        area.setRlng(s2.substring(1,s2.length()-1));
        areaMapper.updateByPrimaryKeySelective(area);
    }



    //asdasdasdada
    /**
     * 通过Id查询风险区域
     * @param id
     * @return
     */
    @Override
    public String findAreaById(Long id) {
        Area t = new Area();
        t.setId(id);
        Area area = areaMapper.selectOne(t);
        String rlat = area.getRlat();
        String rlng = area.getRlng();
        String[] listofrlat = rlat.split(",");
        String[] listofrlng = rlng.split(",");
        String boundary = new String();
        for (int i=0;i<listofrlat.length;i++){
            boundary+=(listofrlat[i]+","+listofrlng[i]+",");
        }
        return boundary + "\b";
    }

    /**
     * 通过地区名字查询风险区域
     * @param name
     * @return
     * 可以任意匹配数据库中包含此信息的字段
     * 通过通用mapper的Example实现sql的Like功能
     */
    @Override
    public String findoneAreabyname(String name) {
        //只会返回一个表示经纬度坐标的字符串
        Example example = new Example(Area.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("placename","%"+name+"%");
        Area area = areaMapper.selectOneByExample(example);
        String rlat = area.getRlat();
        String rlng = area.getRlng();
        String[] listofrlat = rlat.split(",");
        String[] listofrlng = rlng.split(",");
        String boundary = new String();
        for (int i=0;i<listofrlat.length;i++){
            boundary+=(listofrlat[i]+","+listofrlng[i]+",");
        }
        return boundary + "\b";
    }

    @Override
    public List<Area> findAreasbyword(String word) {
        //返回所有包含关键字的地区
        Example example = new Example(Area.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("placename","%"+word+"%");
        List<Area> areas = areaMapper.selectByExample(example);
        return areas;
    }
}
